package com.jpcc.vw.onlineRadio.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.VIPInfoBean;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.jpcc.vw.onlineRadio.utils.CommonSubscriber;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.jpcc.vw.onlineRadio.utils.TrackUtils;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyListActivity extends com.jpcc.vw.onlineRadio.base.BaseActivity {

    @BindView(R.id.layout_search)
    RelativeLayout layout_search;
    @BindView(R.id.tv_file_size)
    TextView tv_file_size;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.imv_top_bottom_line)
    ImageView imv_top_bottom_line;
    @BindView(R.id.imv_back_main)
    ImageView imv_back_main;
    @BindView(R.id.imv_new_search)
    ImageView imv_new_search;
    private IRadio model;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my_list;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        File file = new File(TrackUtils.getTrackDir());
        if (file.exists()) {
            tv_file_size.setText(getFormatSize(getFolderSize(file)));
        } else {
            tv_file_size.setText(0 + "MB");
        }
        tv_file_size.setVisibility(View.INVISIBLE);
        tv_title.setText(getResources().getString(R.string.string_2));
        layout_search.setVisibility(View.INVISIBLE);
        imv_new_search.setVisibility(View.INVISIBLE);
        imv_back_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyListActivity.this.finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        File file = new File(TrackUtils.getTrackDir());
        if (file.exists()) {
            tv_file_size.setText(getFormatSize(getFolderSize(file)));
        } else {
            tv_file_size.setText(0 + "MB");
        }
    }

    @Override
    public void doAtmosphere(boolean rgbOn, String color) {
        Logger.i("doAtmosphere" + "rgbOn-->" + rgbOn);
        Logger.i("doAtmosphere" + "color-->" + color);
        if (rgbOn) {
            if (TextUtils.isEmpty(color)) {
                imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
            } else {
                imv_top_bottom_line.setColorFilter(Color.parseColor(color));
            }
        } else {
            imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
        }
    }

    @Override
    public void setupData() {
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(this).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(this).getHmiAtmosphereLampColor();
                doAtmosphere(atmosphereStatus, color);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public static long getFolderSize(java.io.File file) {
        long size = 0;
        java.io.File[] fileList = file.listFiles();
        if (fileList == null) {
            return 0;
        }
        for (int i = 0; i < fileList.length; i++) {

            if (fileList[i].isDirectory()) {
                size = size + getFolderSize(fileList[i]);

            } else {
                size = size + fileList[i].length();

            }
        }

        return size;
    }


    /**
     * 格式化单位
     *
     * @param size
     * @return
     */
    public static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "B";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }


    private void doValidateCPBind() {
//        if (model == null) {
//            model = new RadioImpl();
//        }
//        model.QueryVIPInfo().subscribe(new CommonSubscriber<VIPInfoBean>() {
//            @Override
//            public void onSuccess(VIPInfoBean response) {
//                Intent intent_paied = new Intent(MyListActivity.this, FavouritePaiedHistoryActivity.class);
//                intent_paied.putExtra("KEY_TYPE", 2);
//                startActivity(intent_paied);
//            }
//
//            @Override
//            public void onFailure(BaseBean response) {
//                if (Constants.ERROR_CODE == response.getCode()) {
//                    ToastUtils.show(R.string.string_107);
//                } else {
//                    ToastUtils.show(response.getStatusMessage());
//                }
//            }
//        });
    }

    @OnClick({R.id.layout_my_favourite, R.id.layout_my_download, R.id.layout_my_paied, R.id.layout_my_history, R.id.layout_back, R.id.layout_search})
    void doClicks(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.layout_my_favourite:
                Intent intent_favourite = new Intent(this, FavouritePaiedHistoryActivity.class);
                intent_favourite.putExtra("KEY_TYPE", 0);
                startActivity(intent_favourite);
                break;
            case R.id.layout_my_download:
                Intent intent_download = new Intent(this, FavouritePaiedHistoryActivity.class);
                intent_download.putExtra("KEY_TYPE", 1);
                startActivity(intent_download);
                break;
            case R.id.layout_my_paied:
                Intent intent_paied = new Intent(MyListActivity.this, FavouritePaiedHistoryActivity.class);
                intent_paied.putExtra("KEY_TYPE", 2);
                startActivity(intent_paied);
                break;
            case R.id.layout_my_history:
                Intent intent_history = new Intent(this, PlayHistoryActivity.class);
                intent_history.putExtra("KEY_TYPE", 3);
                startActivity(intent_history);
                break;
            case R.id.layout_back:
                this.finish();
                break;
            case R.id.layout_search:
                Intent intent_search = new Intent(MyListActivity.this, SearchActivity.class);
                startActivity(intent_search);
                break;
        }
    }
}
